package be.neo4reactome.parser;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

public class InteractionsParser {
	
	public static List<Interaction> read(String file) {

		List<Interaction> interactions = new ArrayList<Interaction>();

		try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;

			while ((line = reader.readLine()) != null) {

                if (line.startsWith("#")) continue;

                String[] proteins = line.split("\\s+");
                String proteinA = "", proteinB ="", interaction = "";

                if (proteins[0].startsWith("UniProt")) {
                    int offset = proteins[0].indexOf(":") + 1;
                    proteinA = proteins[0].substring(offset);
                }

                if (proteins[2].startsWith("UniProt")) {
                    int offset = proteins[2].indexOf(":") + 1;
                    proteinB = proteins[2].substring(offset);
                }

                if(proteins.length >= 5) {
                    if(proteins[4].length() > 0) {
                        interaction = proteins[4];
                    }
                } else {
                    if(proteins[3].length() > 0) {
                        interaction = proteins[3];
                    }
                }

                interactions.add(new Interaction(proteinA, proteinB, interaction));
            }
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return interactions;
	}
	
	public static void main(String[] args) {
		InteractionsParser.read("data/homo_sapiens.interactions.txt");
	}
}
